package com.taxi.driver.module.main.mine.evaluation;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.qianxx.view.HeadView;
import com.taxi.driver.R;
import com.taxi.driver.common.Application;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.config.DriverType;
import com.taxi.driver.module.main.mine.evaluation.dagger.DaggerEvaluationComponent;
import com.taxi.driver.module.main.mine.evaluation.dagger.EvaluationModule;
import com.taxi.driver.module.vo.CommentVO;
import com.taxi.driver.module.vo.EvaluationVO;
import com.taxi.driver.widget.Start;
import com.taxi.driver.widget.XScrollView;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 用户评价
 */
public class EvaluationActivity extends BaseActivity implements EvaluationContract.View,
        XScrollView.XScrollViewListener,SwipeRefreshLayout.OnRefreshListener{
    @BindView(R.id.head_view)
    HeadView head_view;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipe_refresh_layout;
    @BindView(R.id.scrollView)
    XScrollView scrollView;

    @BindView(R.id.start)
    Start start;
    @BindView(R.id.tv_average_score)
    TextView tv_average_score;
    @BindView(R.id.tv_cumulative_score)
    TextView tv_cumulative_score;
    @BindView(R.id.tv_cumulative_order)
    TextView tv_cumulative_order;

    @BindView(R.id.progress_bar5)
    ProgressBar progress_bar5;
    @BindView(R.id.tv_start_level_5_order_number)
    TextView tv_start_level_5_order_number;
    @BindView(R.id.progress_bar4)
    ProgressBar progress_bar4;
    @BindView(R.id.tv_start_level_4_order_number)
    TextView tv_start_level_4_order_number;
    @BindView(R.id.progress_bar3)
    ProgressBar progress_bar3;
    @BindView(R.id.tv_start_level_3_order_number)
    TextView tv_start_level_3_order_number;
    @BindView(R.id.progress_bar2)
    ProgressBar progress_bar2;
    @BindView(R.id.tv_start_level_2_order_number)
    TextView tv_start_level_2_order_number;
    @BindView(R.id.progress_bar1)
    ProgressBar progress_bar1;
    @BindView(R.id.tv_start_level_1_order_number)
    TextView tv_start_level_1_order_number;

    @BindView(R.id.rv_tag)
    RecyclerView rv_tag;
    @BindView(R.id.rv_evaluation)
    RecyclerView rv_evaluation;
    @BindView(R.id.tv_evaluation)
    TextView tv_evaluation;

    @Inject
    EvaluationPresenter mPresenter;

    TagAdapter tagAdapter;
    EvaluationAdapter evaluationAdapter;
    GridLayoutManager gridLayoutManager;
    LinearLayoutManager linearLayoutManager;

    int nowPage = 1;
    final int REFRESH = 0;
    final int LOADMORE = 1;
    int MODEL=REFRESH;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluation);
        ButterKnife.bind(this);
        initView();

        DaggerEvaluationComponent.builder()
                .appComponent(Application.getAppComponent())
                .evaluationModule(new EvaluationModule(this))
                .build().inject(this);

        mPresenter.reqEvaluation();
        mPresenter.reqYesterdayEvaluates(nowPage);

        if(mPresenter.driverType() == DriverType.SPECIAL){
            head_view.setTitle(getResources().getString(R.string.passengers_evaluation));
            tv_evaluation.setText(getResources().getString(R.string.passengers_evaluation));
        }
    }

    public void initView(){
        gridLayoutManager=new GridLayoutManager(this,3);
        rv_tag.setLayoutManager(gridLayoutManager);
        tagAdapter = new TagAdapter(this);
        rv_tag.setAdapter(tagAdapter);
        rv_tag.setFocusable(false);
        rv_tag.setNestedScrollingEnabled(false);

        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_evaluation.setLayoutManager(linearLayoutManager);
        evaluationAdapter = new EvaluationAdapter(this);
        rv_evaluation.setAdapter(evaluationAdapter);
        rv_evaluation.setFocusable(false);
        rv_evaluation.setNestedScrollingEnabled(false);
        scrollView.setXScrollViewListener(this);
        swipe_refresh_layout.setOnRefreshListener(this);
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void showEvaluation(EvaluationVO vo) {
        swipe_refresh_layout.setRefreshing(false);
        scrollView.stopLoadMore();

        tv_average_score.setText(vo.averageScore+"");
        tv_cumulative_score.setText(vo.cumulativeScore+"");
        tv_cumulative_order.setText(vo.cumulativeOrder+"");
        start.setLevel(vo.averageScore);

        progress_bar5.setMax(vo.cumulativeOrder);
        progress_bar4.setMax(vo.cumulativeOrder);
        progress_bar3.setMax(vo.cumulativeOrder);
        progress_bar2.setMax(vo.cumulativeOrder);
        progress_bar1.setMax(vo.cumulativeOrder);

        if(vo.commonStarCountDto != null){
            progress_bar1.setProgress(vo.commonStarCountDto.starOne);
            progress_bar2.setProgress(vo.commonStarCountDto.starTwo);
            progress_bar3.setProgress(vo.commonStarCountDto.starThree);
            progress_bar4.setProgress(vo.commonStarCountDto.starFour);
            progress_bar5.setProgress(vo.commonStarCountDto.starFive);

            tv_start_level_1_order_number.setText(getString(R.string.order_unit,vo.commonStarCountDto.starOne+""));
            tv_start_level_2_order_number.setText(getString(R.string.order_unit,vo.commonStarCountDto.starTwo+""));
            tv_start_level_3_order_number.setText(getString(R.string.order_unit,vo.commonStarCountDto.starThree+""));
            tv_start_level_4_order_number.setText(getString(R.string.order_unit,vo.commonStarCountDto.starFour+""));
            tv_start_level_5_order_number.setText(getString(R.string.order_unit,vo.commonStarCountDto.starFive+""));
        }else{
            tv_start_level_1_order_number.setText(getString(R.string.order_unit,"0"));
            tv_start_level_2_order_number.setText(getString(R.string.order_unit,"0"));
            tv_start_level_3_order_number.setText(getString(R.string.order_unit,"0"));
            tv_start_level_4_order_number.setText(getString(R.string.order_unit,"0"));
            tv_start_level_5_order_number.setText(getString(R.string.order_unit,"0"));
        }

        if(vo.tags != null && vo.tags.size() > 0){
            tagAdapter.clear();
            tagAdapter.addAll(vo.tags);
        }
    }

    @Override
    public void reqFail(int errCode, String errMsg) {
        swipe_refresh_layout.setRefreshing(false);
        scrollView.stopLoadMore();
        if(!TextUtils.isEmpty(errMsg)){
            toast(errMsg);
        }else{
            scrollView.showNoMoreHint();
        }

    }

    @Override
    public void showYesterdayEvaluates(List<CommentVO> vos) {
        swipe_refresh_layout.setRefreshing(false);
        if(vos != null && vos.size() > 0){
            if(MODEL == REFRESH){
                evaluationAdapter.clear();
            }
            evaluationAdapter.addAll(vos);
        }else{
            scrollView.showNoMoreHint();
        }
    }

    @Override
    public void onRefresh() {
        MODEL = REFRESH;
        nowPage = 1;
        mPresenter.reqYesterdayEvaluates(nowPage);
        mPresenter.reqEvaluation();
    }

    @Override
    public void onLoadMore() {
        MODEL = LOADMORE;
        nowPage ++;
        mPresenter.reqYesterdayEvaluates(nowPage);
    }
}
